//封装分类相关代码
import { getCategoryAPI } from "@/apis/category";
import { ref, onMounted } from "vue";
import { onBeforeRouteUpdate, useRoute } from "vue-router";

export function useCategory() {
    const categoryData = ref({});
    const route = useRoute();

    const getCategory = async (id = route.params.id) => {
        // 如何在setup中获取路由参数 useRoute() -> route 等价于this.$route
        const res = await getCategoryAPI(id);
        categoryData.value = res.result;
    };

    onMounted(() => {
        getCategory();
    });

    onBeforeRouteUpdate((to) => {
        getCategory(to.params.id);
    });
    return {
        categoryData,
    };
}
